package com.swipeclose;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.Scroller;

import com.swipeclose.SwipeToClose.SwipeToCloseAction;
/**
 * 横向滑动关闭
 * @author cgh
 *
 */
public class SwipeToCloseLayout extends FrameLayout implements SwipeToCloseAction {
	
	private boolean 		mEnableGesture = true;//是否可滑动
	private boolean 		mToCloseActivity;//是否准备关闭界面
	
	private Scroller		mScroller;
	private SwipeToClose 	mSwipeToClose;//滑动监听
	private Activity 		mActivity;

	private SwipeToCloseLayoutAction mSwipeToCloseLayoutAction;
	
	public SwipeToCloseLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		mScroller 		= new Scroller(context);
		mSwipeToClose 	= new SwipeToClose(context, mScroller);

		mSwipeToClose.setContentView(this);
		mSwipeToClose.setSwipeToCloseAction(this);
		setWillNotDraw(false);//开启onDraw
	}
	
	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		if (mEnableGesture && mSwipeToClose != null && mSwipeToClose.onInterceptTouchEvent(ev)) {
			return true;
		}
		return super.onInterceptTouchEvent(ev);
	}

	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		return (mSwipeToClose != null && mSwipeToClose.onTouchEvent(ev));
	}
	
	/**
	 * 设置是否可以滑动
	 * @param enableGesture
	 */
	public void setEnableGesture(boolean enableGesture) {
		this.mEnableGesture = enableGesture;
	}

	/**
	 * 增加有干扰的横向滑动控件
	 * @param view
	 */
	public void addHorizeScrollviews(View view) {
		this.mSwipeToClose.addHorizeScrollviews(view);
	}

	@Override
	public void computeScroll() {
		super.computeScroll();
		if (mScroller.computeScrollOffset()) {  
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
            postInvalidate();  
        }else{
        	if(mToCloseActivity){
        		if(mSwipeToCloseLayoutAction != null){
        			mSwipeToCloseLayoutAction.onCloseAction();
        		}
        	}
        }
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		if(Math.abs(getScrollX()) <= getWidth()){
			canvas.save();
			int alpha = 160 * (getWidth() - Math.abs(getScrollX())) / getWidth();
			int color = (alpha << 24 | 0x00000000);
			canvas.drawColor(color);
			canvas.restore();

		}
	}

	@Override
	public void startScroll(int startX, int dx, boolean finish) {
		mToCloseActivity = dx < 0;
		mScroller.startScroll(startX, 0, dx, 0, 500 * Math.abs(dx) / getWidth());
		postInvalidate();
	}

	public void attachToActivity(Activity activity){
		mActivity = activity;
		ViewGroup viewGroup = (ViewGroup) activity.findViewById(android.R.id.content);
		View contentView = viewGroup.getChildAt(0);
		viewGroup.removeView(contentView);
		addView(contentView);
		viewGroup.addView(this);
		ChangeThemeUtils.convertActivityFromTranslucent(activity);
	}

	@Override
	public void onScroll() {
		ChangeThemeUtils.convertActivityToTranslucent(mActivity);
	}

	@Override
	public void onTouchDown() {
		ChangeThemeUtils.convertActivityToTranslucent(mActivity);
	}
	
	public void setSwipeToCloseLayoutAction(SwipeToCloseLayoutAction action) {
		this.mSwipeToCloseLayoutAction = action;
	}

	public interface SwipeToCloseLayoutAction {
		/**
		 * 关闭界面
		 */
		void onCloseAction();
	}
	
}
